﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;
using Common;

namespace BLL
{
    public class TimeLineBLL
    {
        private TimeLineDAL dal = new TimeLineDAL();

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="extsql">sql条件</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageCount">条数</param>
        /// <param name="totalPages">总页数</param>
        /// <param name="totalItems">总条数</param>
        /// <returns></returns>
        public ReturnMessages_Page GetPage(string extsql, int pageIndex, int pageCount)
        {
            ReturnMessages_Page rm = new ReturnMessages_Page();
            try
            {
                int totalPages = 0;
                int totalItems = 0;
                if (pageCount < 10)
                {
                    pageCount = 10;
                }
                rm.code = 200;
                rm.pageindex = pageIndex;
                rm.rows = dal.GetPage(extsql, pageIndex, pageCount, ref totalPages, ref totalItems);
                rm.total = totalItems;
                rm.pages = totalPages;
                return rm;
            }
            catch (Exception e)
            {
                rm.code = 500;
                rm.msg = "服务器错误";
                LogHelper.Error(this.GetType().FullName, e.ToString());
                return rm;
            }
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TimeLine GetModel(string id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string column, string where)
        {
            return dal.GetList(column, where);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Add(TimeLine model)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                int count = dal.Add(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "添加成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "添加失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;

        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Update(TimeLine model)
        {
            ReturnMessages rm = new ReturnMessages();


            try
            {
                int count = dal.Update(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "更新成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "更新失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnMessages delete(string id)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                int count = dal.delete(id);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "删除成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "删除失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;
        }

    }
}
